// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgres"
  url      = env("DATABASE_URL")
}

model User {
  id        Int        @id @default(autoincrement())
  email     String
  fio       String
  username  String     @unique
  role      Int        @default(0)
  group     String
  sessions  Session[]
  solutions Solution[]
}

model Session {
  id        String   @id
  userId    Int
  expiresAt DateTime
  user      User     @relation(references: [id], fields: [userId], onDelete: Cascade)
}

model Tag {
  id         Int         @id @default(autoincrement())
  name       String
  Challenges Challenge[]
}

model Challenge {
  id          Int                @id @default(autoincrement())
  name        String
  description String
  tags        Tag[]
  variants    ChallengeVariant[]
  published   Int                @default(1)
}

model ChallengeVariant {
  id             Int        @id @default(autoincrement())
  mdInstructrion String     @db.Text
  test           String     @db.Text
  code           String     @db.Text
  Solutions      Solution[]
  lang           Language   @relation(fields: [languageId], references: [id])
  languageId     Int
  Challenge      Challenge? @relation(fields: [challengeId], references: [id], onDelete: Cascade)
  challengeId    Int?
}

model Solution {
  id                 Int              @id @default(autoincrement())
  challengeVariant   ChallengeVariant @relation(fields: [challengeVariantId], references: [id], onDelete: Cascade)
  challengeVariantId Int
  code               String           @db.Text
  createdAt          DateTime         @default(now())
  updatedAt          DateTime         @updatedAt
  username           String
  user               User             @relation(references: [username], fields: [username], onDelete: Cascade)
  status             Int              @default(0)

  @@unique([challengeVariantId, username])
}

model Language {
  id               Int                @id @default(autoincrement())
  name             String
  ChallengeVariant ChallengeVariant[]
}
